from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, Field


class PermissionBase(BaseModel):
    """权限基础Schema"""
    name: str = Field(..., min_length=2, max_length=100)
    display_name: str = Field(..., min_length=2, max_length=200)
    description: Optional[str] = None
    category: str = Field(..., min_length=2, max_length=50)
    action: str = Field(..., min_length=2, max_length=50)
    resource: str = Field(..., min_length=2, max_length=100)
    is_active: bool = True
    is_system: bool = False


class PermissionCreate(PermissionBase):
    """权限创建Schema"""
    pass


class PermissionUpdate(BaseModel):
    """权限更新Schema"""
    name: Optional[str] = Field(None, min_length=2, max_length=100)
    display_name: Optional[str] = Field(None, min_length=2, max_length=200)
    description: Optional[str] = None
    category: Optional[str] = Field(None, min_length=2, max_length=50)
    action: Optional[str] = Field(None, min_length=2, max_length=50)
    resource: Optional[str] = Field(None, min_length=2, max_length=100)
    is_active: Optional[bool] = None


class PermissionInDBBase(PermissionBase):
    """数据库中的权限基础信息"""
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class Permission(PermissionInDBBase):
    """权限返回Schema"""
    role_count: int = 0


class PermissionInDB(PermissionInDBBase):
    """数据库中的完整权限信息"""
    pass


class PermissionList(BaseModel):
    """权限列表Schema"""
    items: List[Permission]
    total: int
    page: int
    size: int


class PermissionByCategory(BaseModel):
    """按分类分组的权限"""
    category: str
    permissions: List[Permission]


class PermissionTree(BaseModel):
    """权限树形结构"""
    category: str
    actions: List[str]
    resources: List[str]